<?php
class member_index {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	
	function member_index() {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
    }
	
	function displayForm($formvars = array())
	{
		$this->smarty->assign('formvars',$formvars);
		
		
		$memberId="";
		$carrierId="";
		$uim="";
		$type="";
		$userCandCheck="";
		
		if(isset($formvars['actionType'])||isset($formvars['memberId']))
		{
			
			$memberId=$formvars['memberId'];
			$carrierId=$formvars['carrierId'];
			$uim=$formvars['uim'];
			$type=$formvars['type'];
			if(isset($formvars['userCandCheck']))
				$userCandCheck=$formvars['userCandCheck'];
			
			$tag_query = "&memberId=".urlencode($memberId);
			$tag_query .= "&carrierId=".urlencode($carrierId);
			$tag_query .= "&uim=".urlencode($uim);
			$tag_query .= "&type=".urlencode($type);
			if(isset($formvars['userCandCheck']))
			$tag_query .= "&userCandCheck=".urlencode($formvars['userCandCheck']);
		}
		else
		{
			$tag_query ="";
		}
		
		if(isset($formvars['page']) && is_numeric($formvars['page'])){
			$page = $formvars['page'];
		}else{
			$page = 1;
		}
		
		$limit = "limit ".($page-1)*PAGE_SIZE.",".PAGE_SIZE;
	
		
		
		
		/**
		 * 総件数
		 */

		$uimParam='%'.$uim.'%';
		$sql = "SELECT COUNT(memberId) FROM memberMst WHERE 1=1 ";
		if($memberId != "") $sql.=" AND memberId=:memberId";
		if($carrierId != "" && $carrierId != "99") $sql.=" AND carrierId=:carrierId";
		if($uim != "") $sql.=" AND uim like :uim";
		if($type != "" && $type != "99") $sql.=" AND type=:type";
		if($userCandCheck != "") $sql.="  
			AND(
					adclick_date is not null
					AND ( ch_type_date<	adclick_date
					   OR 	ch_type_date is null
					   )
				)";
		
		$stmt = $this->db->prepare($sql);
		if($memberId != "") $stmt->bindParam(':memberId', $memberId);
		if($carrierId != "" && $carrierId != "99") $stmt->bindParam(':carrierId', $carrierId);
		if($uim != "") $stmt->bindParam(':uim', $uimParam);
		if($type != "" && $type != "99") $stmt->bindParam(':type', $type);	
		
		
		$stmt->execute();
		$count =$stmt->fetchColumn();
		$this->smarty->assign('count', $count);
		
		$stmt = null;
		
		/**
		 * 一覧
		 */

		$sql = "SELECT memberMst.*, adspace_mst.type_c showType
		FROM memberMst left join adspace_mst 
			 on memberMst.adspace_id =adspace_mst.adspace_id 
	    WHERE 1=1 ";
		if($memberId != "") $sql.=" AND memberId=:memberId";
		if($carrierId != "" && $carrierId != "99") $sql.=" AND carrierId=:carrierId";
		if($uim != "") $sql.=" AND uim like :uim";
		if($type != "" && $type != "99") $sql.=" AND type=:type";
		if($userCandCheck != "") $sql.="  
			AND(
					adclick_date is not null
					AND ( ch_type_date<	adclick_date
					   OR 	ch_type_date is null
					   )
				)";
		$sql .=" ORDER BY memberId
			    $limit";
		//echo $sql;
		$stmt = $this->db->prepare($sql);
		if (!$stmt) {
    		print_r($this->db->errorInfo());
		}
		if($memberId != "") $stmt->bindParam(':memberId', $memberId);
		if($carrierId != "" && $carrierId != "99") $stmt->bindParam(':carrierId', $carrierId);
		if($uim != "") $stmt->bindParam(':uim', $uimParam);
		if($type != "" && $type != "99") $stmt->bindParam(':type', $type);
		
		$stmt->execute();

		$memberlist = $stmt->fetchAll();
		
		$stmt = null;
		
		$this->smarty->assign('memberlist', $memberlist);
		
		
		
		//$this->smarty->assign('tagQuery', $tag_query);
		//echo $tag_query;
		$this->smarty->assign('paging', $this->smarty->getNavigation("member_index.php",$page,$count,$tag_query));
		$this->smarty->assign('msg', $this->msg);
		$this->smarty->display( 'member_index.tpl' );
	}
	
	function deletBlog($formvars = array())
	{
		$uMemberId=$formvars['uMemberId'];
		$count =$this->db->exec("DELETE FROM memberMst WHERE memberId = $uMemberId");
		if($count>0)
		{
			$this->msg = "会員[$uMemberId]の削除に成功しました";
		}
		else
		{
			$this->msg = "会員[$uMemberId]を削除できません";
		}
	}
	
	function initDropDown()
	{
		
		$this->smarty->assign('carrierIdList', array(
		                       99 => 'すべて',
								0 => 'PC',
                                1 => 'ドコモ',
                                2 => 'AU',
								3 => 'ソフトバンク',
                                4 => 'その他'));
		
		$this->smarty->assign('typeList', array(
								99 => 'すべて',
                                0 => '該当なし',
								1 => 'DL10コース',
								2 => 'DL30コース',
                                3 => 'DL50コース'));
		$this->smarty->assign('typeList2', array(
                                0 => '該当なし',
								1 => 'DL10コース',
								2 => 'DL30コース',
                                3 => 'DL50コース'));
								
	}
	
	function doUpdate($formvars = array())
	{
		$uMemberId = $formvars['uMemberId'];
		$sql = "Update memberMst set type=".$formvars['uType'].",ch_type_date=NOW()
		        WHERE memberId = $uMemberId";
		//echo $sql;
		$count =$this->db->exec($sql);
		if($count>0)
		{
			$this->msg = "会員[$uMemberId]の更新に成功しました";
		}
		else
		{
			$this->msg = "会員[$uMemberId]の更新に失敗しました";
		}
		
	}
}
?>